import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap';
import { UploadFileInfo } from './file-uploader/upload-file-info.class';
import { NotificationAddService } from './notification-add.service';

@Component({
    selector: 'notification-add',
    templateUrl: 'notification-add.component.html',
    styleUrls: ['notification-add.component.scss']
})
export class NotificationAddComponent implements OnInit {
    public ckeditorContent: string = '';
    public notificationTitle;
    public submitResult: string;
    public respMsg: string;
    public valid: boolean = true;
    public uploadUrl: string = '/platform/doc/upload';
    public attachs: UploadFileInfo[] = [];
    public attachNumber: number = 1;
    public attachName: string = 'attach-' + this.attachNumber;
    @ViewChild('msgModal') public modal: ModalDirective;

    public config = {
        language: 'zh-cn',
        uiColor: '#F0F3F4',
        height: '300',
        fontSize_sizes: '16/16px;24/24px;48/48px;',
        toolbarGroups: [
            { name: 'clipboard', groups: ['clipboard', 'undo'] },
            { name: 'editing', groups: ['find', 'selection', 'spellchecker', 'editing'] },
            { name: 'links', groups: ['links'] },
            { name: 'insert', groups: ['insert'] },
            { name: 'forms', groups: ['forms'] },
            { name: 'tools', groups: ['tools'] },
            { name: 'document', groups: ['mode', 'document', 'doctools'] },
            { name: 'others', groups: ['others'] },
            '/',
            { name: 'basicstyles', groups: ['basicstyles', 'cleanup'] },
            { name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi', 'paragraph'] },
            { name: 'styles', groups: ['styles'] },
            { name: 'colors', groups: ['colors'] },
        ],
        removeButtons: 'Underline,Subscript,Superscript,Source',
        filebrowserUploadUrl: '/platform/doc/uploadImage'
    };

    constructor(private notificationAddService: NotificationAddService) { }

    ngOnInit() {
    }

    submitNotification() {
        let attachIds: string[] = [];

        this.attachs.forEach(attch => {
            attachIds.push(attch.ids);
        });
        this.notificationAddService.submitNewNotification(this.notificationTitle, this.ckeditorContent, attachIds)
        .then(resp => {
            if (resp.status === 'success') {
                this.respMsg = '提交成功';
            } else {
                this.respMsg = resp.data;
            }
        });
        this.modal.show();
    }

    handleUploadend(fileInfo: UploadFileInfo) {
        this.attachs.push(fileInfo);
        this.attachNumber += 1;
        this.attachName = 'attach-' + this.attachNumber;
    }

    deleteAttachment(ids: string) {
        let deleteEleIndex = -1;

        for (let index = 0; index < this.attachs.length; index++) {
            if ( this.attachs[index].ids === ids) {
                deleteEleIndex = index;
            }
        }

        this.attachs.splice(deleteEleIndex, deleteEleIndex + 1);
    }

}
